package com.wyb.mapper;

import com.wyb.pojo.Comment;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;


import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author wyb
 * @since 2022-10-23
 */
@Mapper
public interface CommentMapper extends BaseMapper<Comment> {
    //获取文章下的评论
    @Select("select * from sys_comment where topic_id = #{topicId} and is_delete = 0 order by parent_id asc ,create_time desc ")
    List<Comment> selectByTopicId( int topicId);
    //获取父级评论
    @Select("select user_id from sys_comment where id = #{parentId} and is_delete = 0 ")
    Long selectUserIdByParentId(int parentId);
    //获取父级评论
    @Select("select user_id from sys_comment where id = #{replyUserId}")
    Long selectUserIdByReplyUserId(int replyUserId);
    //获取评论的回复
    @Select("select * from sys_comment where parent_id = #{id} and is_delete = 0 ")
    List<Comment> selectReplyByParentId(int id);
//    //获取用户所有评论的回复
//    @Select("select * from sys_comment " +
//            "where parent_id in (select id from sys_comment where user_id = #{userId}) " +
//            "and user_id != #{userId} " +
//            "and is_delete = 0 and is_review = 1 " +
//            "order by create_time desc")
//    List<Comment> selectByUserId(int userId);
//    //获取在回复中的回复
//    @Select("select * from sys_comment " +
//            "where reply_user_id in (select id from sys_comment where user_id = #{userId}) " +
//            "and user_id != #{userId} " +
//            "and is_delete = 0 and is_review = 1 " +
//            "order by create_time desc")
//    List<Comment> selectReplyByUserId(int userId);
    //通过用户的Id获取对用户的回复
    @Select("select distinct a.* from sys_comment a,sys_comment b " +
            "where " +
            "a.user_id != #{userId} " +
            "and b.user_id = #{userId} " +
            "and a.is_delete = 0 and a.is_review = 1 " +
            "and b.is_delete = 0 and b.is_review = 1 " +
            "and (" +
            "a.parent_id = b.id " +
            "or a.reply_user_id = b.id " +
            "or a.topic_id in (select id from sys_article where user_id = #{userId})" +
            ") order by a.create_time desc")
    List<Comment> selectReplyByUserId(int userId);
    //标记评论已读
    @Update("update sys_comment set is_review = 0 where id = #{id} and is_delete = 0 and is_review = 1")
    int updateReply(int id);
}
